package com.swj.modules.system.service.impl;

import com.baomidou.mybatisplus.annotation.OrderBy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swj.common.core.model.SysLoginLog;
import com.swj.common.core.utils.SysStringUtil;
import com.swj.modules.system.mapper.SysLoginLogMapper;
import com.swj.modules.system.model.SysLoginLogQueryVO;
import com.swj.modules.system.service.ISysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysLoginLogServiceImpl
        extends ServiceImpl<SysLoginLogMapper, SysLoginLog>
        implements ISysLoginLogService {

    @Resource
    private SysLoginLogMapper loginLogMapper;

    /**
     * 新增系统登录日志
     *
     * @param logging 访问日志对象
     */
    @Override
    public int insertLoginLog(SysLoginLog logging) {
        return loginLogMapper.insert(logging);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logging 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLoginLog> selectLogininfoList(SysLoginLogQueryVO logging) {
        return loginLogMapper.selectList(getLoginLogWrapper(logging));
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logininfo 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public Page<SysLoginLog> selectLogininfoPage(SysLoginLogQueryVO logininfo) {
        Page<SysLoginLog> page = new Page<>(logininfo.getPageNum(),logininfo.getPageSize());
        return loginLogMapper.selectPage(page,getLoginLogWrapper(logininfo));
    }

    private QueryWrapper<SysLoginLog> getLoginLogWrapper(SysLoginLogQueryVO logininfo) {
        QueryWrapper<SysLoginLog> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(SysStringUtil.isNotEmpty(logininfo.getIpaddr())&&logininfo.getIsLike(), SysLoginLog::getIpaddr, logininfo.getIpaddr())
                .like(SysStringUtil.isNotEmpty(logininfo.getUserName())&&logininfo.getIsLike(), SysLoginLog::getUserName, logininfo.getUserName())
                .eq(SysStringUtil.isNotEmpty(logininfo.getIpaddr())&&!logininfo.getIsLike(), SysLoginLog::getIpaddr, logininfo.getIpaddr())
                .eq(SysStringUtil.isNotEmpty(logininfo.getUserName())&&!logininfo.getIsLike(), SysLoginLog::getUserName, logininfo.getUserName())
                .gt(Objects.nonNull(logininfo.getBeginTime()), SysLoginLog::getCreateTime, logininfo.getBeginTime())
                .lt(Objects.nonNull(logininfo.getEndTime()), SysLoginLog::getCreateTime, logininfo.getEndTime())
                .orderBy("createTime".equals(logininfo.getOrderBy()),"ascending".equals(logininfo.getIsAsc()),SysLoginLog::getCreateTime)
                .orderBy("userName".equals(logininfo.getOrderBy()),"ascending".equals(logininfo.getIsAsc()),SysLoginLog::getUserName)
                .orderBy(SysStringUtil.isEmpty(logininfo.getOrderBy()),false,SysLoginLog::getCreateTime);
        return  wrapper;
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(String[] infoIds) {
        QueryWrapper<SysLoginLog> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(SysStringUtil.isNotEmpty(infoIds),SysLoginLog::getId,infoIds);
        return loginLogMapper.delete(wrapper);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfo() {
        loginLogMapper.delete(new QueryWrapper<SysLoginLog>());
    }
}
